package practices; /**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-06-19 15:15
 * @description: Functions of this class is
 **/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
    private ServerSocket ss;
    private Socket socket;
    private List<Socket> list = new ArrayList<Socket>();
    public Server() {
        try {
            ss = new ServerSocket(10000);
            while (true) {
                socket = ss.accept();
                list.add(socket);
                System.out.println("有人进入了聊天室，现在的聊天室人数为：" + list.size());
                new Thread(new ChatThread(socket, list)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class ChatThread implements Runnable{
        private Socket socket;
        private List<Socket> socketList;
        private BufferedReader bufferedReader;

        public ChatThread(Socket socket, List<Socket> socketList) {
            super();
            this.socket = socket;
            this.socketList = socketList;
        }
        private void  preRun(){
            InputStreamReader r=null;
            try {
                r = new InputStreamReader(this.socket.getInputStream());
                this.bufferedReader=new BufferedReader(r);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        private PrintWriter getSocketPrintWriter(Socket socket){
            OutputStream os=null;
            try {
                os = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            OutputStreamWriter osw=new OutputStreamWriter(os);
            BufferedWriter bw=new BufferedWriter(osw);
            return new PrintWriter(bw, true);
        }
        private void broadcast(String msg){
            for(Socket s : socketList){
                PrintWriter pw=this.getSocketPrintWriter(s);
                pw.println(msg);
            }
        }
        @Override
        public void run() {
            String msg="";
            this.preRun();
            while(true){
                try {
                    msg=this.bufferedReader.readLine();
                    System.out.println(msg);
                    this.broadcast(msg);
                } catch (IOException e) {
                    try {
                        this.bufferedReader.close();
                        this.socket.close();
                        this.socketList.remove(this.socket);
                        this.broadcast("有人离开了聊天室，剩余人数为:" +socketList.size());
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    e.printStackTrace();
                    return ;
                }
            }
        }
    }
    public static void main(String[] args) {
        System.out.println("这里是聊天室接收服务端，聊天室开启，开始聊天......");
        new Server();
    }

}